javascript oop、instanceof 和基类
全部标签 publicCArm(Vector3at,stringname):base(name){}除了:base(name)之外,还有其他方法可以在括号内调用基础父构造函数吗?我不确定这是否是另一种语言,但我记得在构造函数中使用类似super();的方法来调用基类。谢谢。 最佳答案 不可以,您不能在C#中的构造函数体内调用基类构造函数。您可能正在考虑Java的语法。您可以通过调用方法来模拟所需的行为。只需确保在调用虚拟方法时非常小心! 关于C#调用基类构造函数,我们在StackOverflow上
假设我有一个SpaceShip类,如下所示:publicclassSpaceShip{publicSpaceShip(){}publicSpaceShip(IRocketFuelSourcefuelSource){}}我想使用TypeBuilder在运行时创建一个继承自SpaceShip的类型,并为SpaceShip中的每个类型定义一个构造函数。除了将参数传递给父级(“直通”构造函数)之外,我不需要构造函数实际做任何事情。例如,如果用C#表示,生成的类型将如下所示:publicclassSpaceShipSubClass:SpaceShip{publicSpaceShipSubClas
在C#中,我有基类Product和派生类Widget。产品包含静态方法MyMethod()。我想从静态方法Widget.MyMethod()调用静态方法Product.MyMethod()。我不能使用base关键字,因为它只适用于实例方法。我可以显式调用Product.MyMethod(),但如果我稍后将Widget更改为从另一个类派生,我必须修改该方法。C#中是否有一些类似于base的语法允许我从派生类的静态方法调用基类的静态方法? 最佳答案 static方法基本上是一种从面向对象概念回退的方法。因此,它们在继承层次结构中不是很灵
我有一个基类publicclassA{publicstrings1;publicstrings2;}我还有一个派生类:publicclassB:A{publicstrings3;}假设我的程序创建了一个类A的实例。AaClassInstance=newA();设置了一些参数:aClassInstance.s1="string1";aClassInstance.s2="string2";此时我想创建一个类B的实例。但我希望B已经具有我的类A实例的值。这没有用:publicBbClassInstance=newB():bClassInstance=(B)aClassInstance;这也没
在MSTest中创建通用基测试类并从中继承时,我无法运行所有继承类的测试。BaseDependencyPropertyFactoryTest位于Whathecode.PresentationFramework.Tests程序集中。它是通用基类。(BaseDependencyPropertyFactoryTest)两个程序集都有一个继承自该基类的测试,称为DependencyPropertyFactoryTest。继承类所做的只是传递一个特定类型的参数。[TestClass]publicclassDependencyPropertyFactoryTest:BaseDependencyPr
以下C#代码无法编译:publicclassA{publicinterfaceB{}}publicclassC:A,C.B//Errorgivenhere:Thetypename'B'doesnotexistinthetype'C'.{}publicclassD:C.B//Compileswithoutproblemsifwecommentout'C.B'above.{}根据C#4.0规范(第10.1.4.1段),此行为是正确的:WhiledeterminingthemeaningofthedirectbaseclassspecificationAofaclassB,thedirect
我正在尝试通过覆盖索引器来添加在List>中查找元素的功能。usingSystem;usingSystem.Collections.Generic;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;namespaceConsoleApplication2{publicclassMyList:List>{publicintthis[stringkey]{get{returnbase.Single(item=>item.Key==key).Value;}}}}由于某种原因,编译器将抛出此错误:'System.Coll
我有一个基类,它有一个属性,它有一个setter方法。有没有一种方法可以从派生类调用基类中的setter并向其添加更多功能,就像我们使用base关键字处理重写方法一样。抱歉,我应该添加一个示例。这是一个例子。希望我做对了:publicclassA{publicabstractvoidAProperty{set{//doingsomethinghere}}}publicclassB:A{publicoverridevoidAProperty{set{//howtoinvokethebaseclasssetterhere//thenaddsomemorestuffhere}}}
对于“大型库”,instanceof的性能如何?它是否像这样沿着原型(prototype)链一个接一个向上移动?://..var_=john.constructor;while(true){if(_===Human){returntrue;}_=_.prototype.constructor}returnfalse;//..与在每个对象的属性中存储一个唯一的接口(interface)ID号相比,instanceof是否相对较差。 最佳答案 是的,类似的东西。这是来自specification的相关部分:11.8.6Theinstan
使用ES6class语法,我想知道当有多个继承链时,为什么instanceof运算符对继承链不起作用?(optionalread)Howinstanceofoperatorworks?InobjinstanceofConstructor,theinstanceofoperatorchecksifthe'prototype'propertyoftheConstructorfunctionispresentintheprototypechainoftheobj.Ifitispresent,returntrue.Otherwise,false.在下面的代码片段中,BTError继承自Erro